﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Store
{
    public class ModuleDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHStore);
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="order"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateOrder(int order, int id)
        {
            var sql = "UPDATE [Permission].[Module] SET [Sort] = @M_Order WHERE [ModuleID]=@M_Id";
            SqlParameter[] parameters = {
					new SqlParameter("@M_Order", order),
                    new SqlParameter("@M_Id", id)
			};
            int rows = DbHelperSQL.ExecuteSql(sql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// 是否存在该记录
        /// </summary>
        public bool ExistsByType(int typeid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[Module]");
            strSql.Append(" where ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ModuleTypeID",typeid)
			};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public int GetCurrentOrder()
        {
            return DbHelperSQL.GetMaxID("Sort", "[Permission].[Module]");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ModuleName, int typeid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[Module]");
            strSql.Append(" where ModuleName=@ModuleName and ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleName",ModuleName),
                    new SqlParameter("@ModuleTypeID",typeid)
			};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ModuleID, string ModuleName, int typeid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[Module]");
            strSql.Append(" where ModuleID<>@ModuleID and ModuleName=@ModuleName and ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleName",ModuleName),
                    new SqlParameter("@ModuleID",ModuleID),
                     new SqlParameter("@ModuleTypeID",typeid)
			};
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ModuleEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[Module](");
            strSql.Append("ModuleTypeID,ModuleName,ModuleTag,URL,Sort,Display,Disabled,Description)");
            strSql.Append(" values (");
            strSql.Append("@ModuleTypeID,@ModuleName,@ModuleTag,@URL,@Sort,@Display,@Disabled,@Description)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleTypeID", SqlDbType.Int,4),
					new SqlParameter("@ModuleName", SqlDbType.NVarChar,30),
					new SqlParameter("@ModuleTag", SqlDbType.NVarChar,50),
					new SqlParameter("@URL", SqlDbType.NVarChar,200),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Display", SqlDbType.TinyInt,1),
					new SqlParameter("@Disabled", SqlDbType.TinyInt,1),
					new SqlParameter("@Description", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ModuleTypeID;
            parameters[1].Value = model.ModuleName;
            parameters[2].Value = model.ModuleTag;
            parameters[3].Value = model.URL;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.Display;
            parameters[6].Value = model.Disabled;
            parameters[7].Value = model.Description;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModuleEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[Module] set ");
            strSql.Append("ModuleTypeID=@ModuleTypeID,");
            strSql.Append("ModuleName=@ModuleName,");
            strSql.Append("ModuleTag=@ModuleTag,");
            strSql.Append("URL=@URL,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("Display=@Display,");
            strSql.Append("Disabled=@Disabled,");
            strSql.Append("Description=@Description");
            strSql.Append(" where ModuleID=@ModuleID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleTypeID", SqlDbType.Int,4),
					new SqlParameter("@ModuleName", SqlDbType.NVarChar,30),
					new SqlParameter("@ModuleTag", SqlDbType.NVarChar,50),
					new SqlParameter("@URL", SqlDbType.NVarChar,200),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Display", SqlDbType.TinyInt,1),
					new SqlParameter("@Disabled", SqlDbType.TinyInt,1),
					new SqlParameter("@Description", SqlDbType.NVarChar,50),
					new SqlParameter("@ModuleID", SqlDbType.Int,4)};
            parameters[0].Value = model.ModuleTypeID;
            parameters[1].Value = model.ModuleName;
            parameters[2].Value = model.ModuleTag;
            parameters[3].Value = model.URL;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.Display;
            parameters[6].Value = model.Disabled;
            parameters[7].Value = model.Description;
            parameters[8].Value = model.ModuleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ModuleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[Module] ");
            strSql.Append(" where ModuleID=@ModuleID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleID", SqlDbType.Int,4)
			};
            parameters[0].Value = ModuleID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ModuleIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[Module] ");
            strSql.Append(" where ModuleID in (" + ModuleIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModuleEntity GetModel(int ModuleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ModuleID,ModuleTypeID,ModuleName,ModuleTag,URL,Sort,Display,Disabled,Description from [Permission].[Module] ");
            strSql.Append(" where ModuleID=@ModuleID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleID", SqlDbType.Int,4)
			};
            parameters[0].Value = ModuleID;

            ModuleEntity model = new ModuleEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModuleEntity DataRowToModel(DataRow row)
        {
            ModuleEntity model = new ModuleEntity();
            if (row != null)
            {
                if (row["ModuleID"] != null && row["ModuleID"].ToString() != "")
                {
                    model.ModuleID = int.Parse(row["ModuleID"].ToString());
                }
                if (row["ModuleTypeID"] != null && row["ModuleTypeID"].ToString() != "")
                {
                    model.ModuleTypeID = int.Parse(row["ModuleTypeID"].ToString());
                }
                if (row["ModuleName"] != null)
                {
                    model.ModuleName = row["ModuleName"].ToString();
                }
                if (row["ModuleTag"] != null)
                {
                    model.ModuleTag = row["ModuleTag"].ToString();
                }
                if (row["URL"] != null)
                {
                    model.URL = row["URL"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["Display"] != null && row["Display"].ToString() != "")
                {
                    model.Display = int.Parse(row["Display"].ToString());
                }
                if (row["Disabled"] != null && row["Disabled"].ToString() != "")
                {
                    model.Disabled = int.Parse(row["Disabled"].ToString());
                }
                if (row["Description"] != null)
                {
                    model.Description = row["Description"].ToString();
                }
            }
            return model;
        }

    }
}
